/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.collector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import za.org.foobar.famon.model.*;
import za.org.foobar.famon.model.StackTraceElement;
import za.org.foobar.famon.util.DB;
import za.org.foobar.famon.util.DBQuery;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * @author Carl Crous
 */
public class DeleteDataServlet extends HttpServlet {
    private final Log log = LogFactory.getLog(getClass());

    private static PrintWriter wr;
    public void println(String str) {
        log.info(str);
        wr.println(str);
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        wr = new PrintWriter(response.getOutputStream());
        wr.println();
        wr.println();
        wr.println("<html><body><pre>");
        try {
            deleteAll(SourceLocation.class);
            deleteAll(StackTraceElement.class);
            deleteAll(LoggingEvent.class);
            deleteAll(Failure.class);
            deleteAll(ApplicationInstance.class);
            deleteAll(ApplicationVersion.class);
            deleteAll(Application.class);
        } catch (Exception e) {
            println("Exception: " + e.getMessage());
            e.printStackTrace(wr);
            log.info("", e);
        } finally {
            wr.println("</pre></body></html>");
            wr.close();
            response.getOutputStream().close();
            DB.closePM();
        }
    }

    private void deleteAll(Class cls) {
        long cnt;
        do {
            DBQuery q = DB.createDBQuery(cls);
            cnt = q.getQuery().deletePersistentAll();
            if (cnt > 0) {
                println("Deleted " + cnt + " from " + cls.getSimpleName());
            }
            try {
                Thread.sleep(20);
            } catch (InterruptedException ignored) {
            }
        } while (cnt > 0);
    }

}
